﻿using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace HBase {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：MyReflectionUtil                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2021/11/3 13:51:01                        
   *└────────────────────────────────────────────────┘
   **/
    public class MyReflectionUtil {

        static public void setFValue(Object obj ,String name ,Object value) {
            Type t = obj.GetType();
            FieldInfo info = t.GetField(name);
            info.SetValue(obj ,value);
        }

        static public Object getFValue(Object obj ,String name) {
            Type t = obj.GetType();
            FieldInfo info = t.GetField(name);
            return info.GetValue(obj);
        }
        public static BsonDocument getGSBsonDocument(Object s)
        {
            // get source PropertyInfos
            PropertyInfo[] pps = GetPropertyInfos(s.GetType());
            BsonDocument bd = new BsonDocument();

            foreach (var pp in pps)
            {
                object v = pp.GetValue(s, null);
                if (v == null)
                {
                    continue;
                }
                bd[pp.Name] = obj2BsonValue(v);

            }
            return bd;  

        }
        static public BsonDocument getBsonDocument(Object obj)
        {
            BsonDocument bd= new BsonDocument();
            Type t = obj.GetType();
      
            FieldInfo[] fields = t.GetFields();
            Type tmp = typeof(DateTime);
            foreach (FieldInfo fi in fields)
            {
                Object v = fi.GetValue(obj);
         
                if (v == null) {
                    continue;
                }
                bd[fi.Name] = obj2BsonValue(v);

            }

            return bd;
        }

        static  BsonValue obj2BsonValue(object v) {

            if (v == null)
            {
                return null;
            }
            if (v is DateTime)
            {
                return (DateTime)v;
            }
            else if (v is ValueType)
            {
                return BsonValue.Create(v);

            }
            else if (v is string)
            {
                return (string)v;
            }
            else
            {

                return getBsonDocument(v);
            }

        }


        public static PropertyInfo[] GetPropertyInfos(Type type) {
            return type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
        }

        public static void AutoMappingGS<S, T>(S s ,T t) {
            // get source PropertyInfos
            PropertyInfo[] pps = GetPropertyInfos(s.GetType());
            // get target type
            Type target = t.GetType();

            foreach( var pp in pps ) {
                PropertyInfo targetPP = target.GetProperty(pp.Name);
                object value = pp.GetValue(s ,null);

                if( targetPP != null && value != null ) {
                    targetPP.SetValue(t ,value ,null);
                }
            }

        }
    }
}
